package juc.ProducerCnosumer;

/**
 * 生产者
 * @author 马迪迪
 * @description: TODO
 * @data 2025年09月15日22时41分
 */
public class Consumer implements Runnable {
    private final Store store;
    // 消费者购买间隔（1-3秒，模拟真实消费频率）
    private static final long CONSUME_INTERVAL = (long) (1000 + Math.random() * 2000);

    public Consumer(Store store) {
        this.store = store;
    }

    @Override
    public void run() {
        while (store.isRunning()) {
            try {
                // 模拟消费间隔
                Thread.sleep(CONSUME_INTERVAL);
                // 执行购买
                store.consume();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                break;
            }
        }
        System.out.printf("[%s] 停止消费%n", Thread.currentThread().getName());
    }
}
